package cn.edu.zafu.demo.ui.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hannesdorfmann.mosby.mvp.MvpActivity;

import cn.edu.zafu.demo.R;
import cn.edu.zafu.demo.ui.presenter.LoginPresenter;
import cn.edu.zafu.demo.ui.view.LoginView;

public class LoginActivity extends MvpActivity<LoginView, LoginPresenter> implements View.OnClickListener, LoginView {
    private EditText etAccount;
    private EditText etPassword;
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etAccount = (EditText) findViewById(R.id.accout);
        etPassword = (EditText) findViewById(R.id.password);
        btnLogin = (Button) findViewById(R.id.login);
        btnLogin.setOnClickListener(this);

    }

    @NonNull
    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenter();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                onLogin();
                break;

        }
    }

    private void onLogin() {
        String username = etAccount.getText().toString();
        String passowrd = etPassword.getText().toString();
        getPresenter().login(username, passowrd);
    }


    @Override
    public void onLoginSuccess() {
        Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLoginFailure(int code) {
        switch (code) {
            case LoginView.USERNAME_OR_PASSWORD_EMPTY:
                Toast.makeText(this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
                break;
            case LoginView.USERNAME_OR_PASSWORD_ERROR:
                Toast.makeText(this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                break;
            case LoginView.SERVER_ERROR:
                Toast.makeText(this, "服务器错误", Toast.LENGTH_SHORT).show();
                break;
        }

    }
}
